package com.lemon.core.utils;

import cn.hutool.core.util.NumberUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author 李猛
 * @datetime 2024/1/12 10:24
 * @description 类对象
 */
public class DateUtil {
    public final static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");
    public final static SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月");
    public final static SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    public final static SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static SimpleDateFormat format5 = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat format6 = new SimpleDateFormat("HH:mm:ss");
    public final static SimpleDateFormat format7 = new SimpleDateFormat("yyyyMMdd");
    public final static SimpleDateFormat format8 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

    public static String toString(Date date) {
        return format4.format(date);
    }

    /**
     * 在当前日期加上一定的时间
     *
     * @param date
     * @param time
     * @param unit
     * @return
     */
    public static Date add(Date date, long time, TimeUnit unit) {
        Date result = new Date();
        switch (unit) {
            case MILLISECONDS:
                result.setTime(date.getTime() + time);
                break;
            case SECONDS:
                result.setTime(date.getTime() + (time * 1000));
                break;
            case MINUTES:
                result.setTime(date.getTime() + (time * 1000 * 60));
                break;
            case HOURS:
                result.setTime(date.getTime() + (time * 1000 * 60 * 60));
                break;
            case DAYS:
                result.setTime(date.getTime() + (time * 1000 * 60 * 60 * 24));
                break;
            default:
                result.setTime(date.getTime());
        }
        return result;
    }

    /**
     * 在当前日期加上一定的时间
     *
     * @param date
     * @param time
     * @param unit
     * @return
     */
    public static Date add(Date date, String time, TimeUnit unit) {
        return add(date, NumberUtil.parseLong(time), unit);
    }
}
